//
//  HWVoiceRoomGiftTopView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/27.
//

import UIKit

class HWVoiceRoomGiftTopView: UIView {
    
    var sendToLabel:UILabel!
    var selectAllButton:UIButton!
    var models:[HWVoiceRoomSeatModel] = Array()
    var collectionView:UICollectionView!
    
    let collectionProtocol = HWVoiceRoomGiftTopProtocol()
    var didSelectUser:((_:[HWVoiceRoomSeatModel])->())?
    var seatModels:[HWVoiceRoomSeatModel]?{
        didSet {
            models.removeAll()
            for mod in seatModels ?? [] {
                if mod.isUsed == true {
                    models.append(mod)
                }
            }
            collectionProtocol.seatModels = models
            collectionView.reloadData()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
        
        initFrame()
        
        addAction()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension HWVoiceRoomGiftTopView {
    private func initUI() -> Void {
        
        sendToLabel = HWLabel.createLabel(title: "送给", color: "#E7E7E7", font: 12)
        addSubview(sendToLabel)
        
        selectAllButton = UIButton(type: UIButton.ButtonType.custom)
        selectAllButton.setTitle("全选", for: UIControl.State.normal)
        selectAllButton.backgroundColor = .colorWithHexString(hexString: "#FDDD00")
        selectAllButton.titleLabel?.font  = UIFont.boldSystemFont(ofSize: 12)
        selectAllButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: UIControl.State.normal)
        selectAllButton.layer.cornerRadius = 12
        selectAllButton.addTarget(self, action: #selector(selectAllButtonClick(_:)), for: .touchUpInside)
        addSubview(selectAllButton)
        
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: 30, height: 40)
        layout.minimumLineSpacing = 15
//        layout.minimumInteritemSpacing = 34*HWCommonly.widScaleValue()
        layout.sectionInset = .init(top: 0, left: 0, bottom: 0, right: 0)
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(HWVoiceRoomGiftUserCell.self, forCellWithReuseIdentifier: "HWVoiceRoomGiftUserCell")
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = UIColor.clear
        collectionView.showsHorizontalScrollIndicator = false
        addSubview(collectionView)
    }
    
    private func initFrame() -> Void {
        sendToLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.centerY.equalToSuperview()
        }
        
        selectAllButton.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(24)
            make.width.equalTo(47)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(sendToLabel.snp.right).offset(10)
            make.top.equalTo(5)
            make.bottom.equalTo(0)
            make.right.equalTo(selectAllButton.snp.left).offset(-10)
        }
    }
}
extension HWVoiceRoomGiftTopView {
    @objc func selectAllButtonClick(_ button:UIButton) -> Void {
        for mod in self.models{
            mod.selected = true
        }
        collectionView.reloadData()
        self.didSelectUser?(self.models)
    }
    
    func addAction() -> Void {
        collectionProtocol.didSelectUser = {
            user in
            self.collectionView.reloadData()
            self.didSelectUser?([user])
        }
    }
}
